package com.springmvc.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.net.URL;

public class XmlParser {
    public static String getBasePackage(String xml) {
        SAXReader reader=new SAXReader();
        InputStream resourceAsStream = XmlParser.class.getClassLoader().getResourceAsStream(xml);
//        URL resource = XmlParser.class.getClassLoader().getResource("");
//        URL resource2 = XmlParser.class.getClassLoader().getResource("springmvc.xml");
//        URL resource3 = XmlParser.class.getClassLoader().getResource("/");
//        System.out.println(resource.toString() + "   res1");
//        System.out.println(resource2.toString() + "   res2");
//        System.out.println(resource3.toString() + "   res3");
        //2.调用read的方法
        Document doc= null;
        String result = "";
        try {
            doc = reader.read(resourceAsStream);
            Element root=doc.getRootElement();
            result = root.element("component-scan").attribute("base-package").getText();

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }
}
